package com.example.dao.impl;

import com.example.dao.CollectionDao;
import com.example.model.Collection;
import com.example.model.User;
import com.example.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CollectionDaoImpl implements CollectionDao {
    @Override
    public List<Collection> getUsersByCondition(Map<String, String> params) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Collection collection = null;
        String cname = params.get("cname");
        String cstate = params.get("cstate");
        int currentPage = Integer.parseInt(params.get("currentPage"));
        int pageSize = Integer.parseInt(params.get("pageSize"));
        int dataOff = (currentPage -1) * pageSize;
        List<Collection> collectionList = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            String sql = "select id,cname,price,releasenum,residual,startdate,enddate from tb_collection where 1=1 ";
            if(cname != null && !cname.equals("")) {
                sql += " and cname = '"+cname + "'";
            }
            String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            if(cstate != null && !cstate.equals("")) {
                int _state = Integer.parseInt(cstate);
                switch (_state){
                    case 0:
                        sql += " and startdate >= '"+ str + "'";
                        break;
                    case 1:
                        sql += " and residual = 0 ";
                        break;
                    case 2:
                        sql += " and startdate < '"+ str + "' and enddate > '" + str + "' and residual >0";
                        break;
                    case 3:
                        sql += " and enddate <= '"+ str + "'";
                        break;
                }
            }
            sql += " limit ?,?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,dataOff);
            ps.setInt(2,pageSize);
            rs = ps.executeQuery();
            while (rs.next()){
                collection = new Collection();
                collection.setId(rs.getInt("id"));
                collection.setCname(rs.getString("cname"));
                collection.setPrice(rs.getDouble("price"));
                collection.setReleasenum(rs.getInt("releasenum"));
                collection.setResidual(rs.getInt("residual"));
                collection.setStartdate(rs.getDate("startdate"));
                collection.setEnddate(rs.getDate("enddate"));
                collectionList.add(collection);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return collectionList;
    }

    @Override
    public int getTotal(Map<String, String> params) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int total = 0;
        String cname = params.get("cname");
        String cstate = params.get("cstate");
        try {
            conn = DBUtil.getConnection();
            String sql = "select count(*) from tb_collection where 1=1 ";
            if(cname != null && !cname.equals("")) {
                sql += " and cname = '"+cname + "'";
            }
            String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            if(cstate != null && !cstate.equals("")) {
                int _state = Integer.parseInt(cstate);
                switch (_state){
                    case 0:
                        sql += " and startdate >= '"+ str + "'";
                        break;
                    case 1:
                        sql += " and residual = 0 ";
                        break;
                    case 2:
                        sql += " and startdate < '"+ str + "' and enddate > '" + str + "' and residual >0";
                        break;
                    case 3:
                        sql += " and enddate <= '"+ str + "'";
                        break;
                }
            }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                total = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return total;
    }
}
